package com.chen.spring.cloud.miniapp.api.feign.controller;

import com.chen.entity.User;
import com.chen.spring.cloud.miniapp.api.feign.service.IndexService;
import com.chen.spring.cloud.miniapp.api.feign.service.UserService;
import com.chen.util.MapperUtils;
import com.chen.util.R;
import com.chen.vo.ServiceTelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/Index")
public class IndexController {

    @Autowired
    private IndexService indexService;

    @Autowired
    private UserService userService;


    @RequestMapping("/getServiceTel")
    public R getServiceTel(){
        Map<String, Object> data = new HashMap<>();
        String result = indexService.getServiceTel();
        try {
            data.put("data",MapperUtils.json2list(result, ServiceTelVo.class));
        } catch (Exception e){
            e.printStackTrace();
            return R.error("获取数据失败");
        }

        return R.ok(data);
    }

    @RequestMapping("/index")
    public R index(String wx_openid,Integer level){

        int u_id = 0;
        if (!StringUtils.isEmpty(wx_openid)){
            String userjson = userService.getUidByOpenid(wx_openid);
            try {
                u_id = MapperUtils.json2pojo(userjson, User.class).getU_id();
            } catch (Exception e){
                e.printStackTrace();
            }
        }

        String result = indexService.getIndex(u_id);
        try {
            return R.ok(MapperUtils.json2map(result));
        } catch (Exception e){
            e.printStackTrace();
        }

        return R.error(500,"失败了");
    }
}
